package com.ncucoder.soms.controller;

import com.ncucoder.soms.dto.DisplayProviderDTO;
import com.ncucoder.soms.dto.ResponseDTO;
import com.ncucoder.soms.entity.Provider;
import com.ncucoder.soms.service.ProviderService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author <a href="https://www.ncucoder.com">hsowan</a>
 * @date 2019-07-10
 **/
@RestController
@RequestMapping("/api/v1")
public class ProviderController {

    private final ProviderService providerService;

    public ProviderController(ProviderService providerService) {
        this.providerService = providerService;
    }

    @ApiOperation("分页获取供应商信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码, 默认值: 1", paramType = "query"),
            @ApiImplicitParam(name = "page_size", value = "每页展示的数目, 默认值: 10", paramType = "query")
    })
    @GetMapping("/providers")
    public ResponseDTO listProviders(@RequestParam(defaultValue = "1", required = false) int page, @RequestParam(name = "page_size", defaultValue = "10", required = false) int pageSize) {
        int maxPageSize = 20;
        if (page <= 0 || pageSize <= 0 || pageSize > maxPageSize) {
            return new ResponseDTO(ResponseDTO.BAD_REQUEST, "参数范围不合法");
        }

        return new ResponseDTO(ResponseDTO.OK, providerService.listProviders(page, pageSize));

    }

    @GetMapping("/providers/count")
    @ApiOperation("获取供应商总数")
    public ResponseDTO count() {

        int count = providerService.countProviders();
        if (count >= 0) {
            return new ResponseDTO(ResponseDTO.OK, count);
        }
        return new ResponseDTO(ResponseDTO.SERVER_ERROR, ResponseDTO.SERVER_ERROR_MASSAGE);

    }

    @ApiOperation("删除供应商")
    @ApiImplicitParam(name = "id", value = "供应商id", required = true, dataTypeClass = Long.class, paramType = "query")
    @DeleteMapping("/provider")
    public ResponseDTO delete(@RequestParam Long id){
        if (id == null || id <= 0) {
            return new ResponseDTO(ResponseDTO.BAD_REQUEST, "删除失败");
        }
        if (providerService.deleteProviderById(id) != 1) {
            return new ResponseDTO(ResponseDTO.SERVER_ERROR, ResponseDTO.SERVER_ERROR_MASSAGE);
        }
        return new ResponseDTO(ResponseDTO.OK, "删除成功");
    }

    @PostMapping("/provider")
    @ApiOperation("添加供应商")
    public ResponseDTO addProvider(@RequestBody Provider provider) {
        int r = providerService.addProvider(provider);
        if (r == 1) {
            return new ResponseDTO(ResponseDTO.CREATED, "添加成功");
        } else {
            return new ResponseDTO(ResponseDTO.BAD_REQUEST, "添加失败");
        }
    }

    @ApiOperation("修改供应商")
    @PostMapping("/provider/update")
    public ResponseDTO updateProvider(@RequestBody Provider provider) {
        int r = providerService.updateProvider(provider);
        if (r == 1) {
            return new ResponseDTO(ResponseDTO.OK, "修改成功");
        } else {
            return new ResponseDTO(ResponseDTO.BAD_REQUEST, "修改失败");
        }
    }

    @GetMapping("/providers/all")
    @ApiOperation("获取所有的供应商的名称和ID")
    public ResponseDTO listAllProviders() {
        List<DisplayProviderDTO> displayProviders = providerService.listAllProviderNames();
        return new ResponseDTO(ResponseDTO.OK, displayProviders);
    }
}
